import sys
import os
import json
from datetime import datetime as dt
from MyData import MyData

# 读信息
if len(sys.argv) > 1:
    username = sys.argv[1]
else:
    username = "15399900725"

# 新建打卡类
dk = MyData(username)

# 检查是否有存档
if os.path.exists(username):
    dk.read_saved_info(username)
    print("%s 读取%s的存档" % (dt.now(), username))
    
# 检查是否已打卡以验证access_token是否正确
has_recorded = dk.req_has_recorded()
if has_recorded['status'] == False:
    dk.req_subUser()
    has_recorded = dk.req_has_recorded()

count = 1
for page in range(1,100):
    record_list = dk.req_get_record_list(page)
    if record_list != None:
        for e in record_list['data']:
            print ("%04d\t%s\t%s\t%s\t" % (count, e['onOffdutyTime'], e['attendanceAddr'], e['picurl']))
            if len(sys.argv) > 2 and os.path.exists(sys.argv[2]):
                dk.req_get_file(e['picurl'], "%s/%04d-%s-%s.jpg" % (sys.argv[2], count, e['onOffdutyTime'][0:10], e['onOffdutyTime'][11:13]))
            count = count + 1
    else:
        break